# -*- coding: utf-8 -*-
import scrapy, json
from ..items import TianmaoguojiItem


class TianmaoSpider(scrapy.Spider):
    name = 'tianmao'
    start_urls = [
        'https://ju.taobao.com/json/tg/ajaxGetItemsV2.json?_ksTS=1550460051080_243&244&psize=20&label=sys_ict_13186&type=0&isLock=1&timeFilter=now&page=1']

    def parse(self, response):
        data = json.loads(response.text)['itemList']
        for dd in data:
            name = dd['name']['title']
            miaoshu = ','.join(dd['merit']['desc'])
            miaosu = dd['baseinfo']['leftTime']
            xianjia = dd['price']['actPrice']
            yuanjia = dd['price']['origPrice']
            guojia_dd = dd['global']['country']
            guojia = guojia_dd.get('cnName', '空')
            yishou = dd['remind']['soldCount']
            shangpinid = dd['baseinfo']['itemId']
            img_src = 'https:' + dd['baseinfo']['picUrl']
            item = TianmaoguojiItem()
            item['name'] = name
            item['miaoshu'] = miaoshu
            item['miaosu'] = miaosu
            item['xianjia'] = xianjia
            item['yuanjia'] = yuanjia
            item['guojia'] = guojia
            item['yishou'] = yishou
            item['shangpinid'] = shangpinid
            item['img_src'] = img_src
            print(item)
            yield item
